<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/10/26
 * Time: 9:43
 */

namespace app\api\controller;


use language\Languagemsg;
use think\Db;

class Home extends Base
{
    private $user_id; // 用户ID
    private $page; // 页码
    private $create_time;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->msg_language = new Languagemsg();
        $this->language = input('language') ? input('language') : 0;
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id, $time, $value, $reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301, 'msg'=>$this->msg_language->otherLogin[$this->language]]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->msg_language->frost[$this->language]]);
            exit;
        }
    }
    
    /**
     *  轮播图
     * @return \think\response\Json
     */
    public function banner(){
        $banner = Db::table('up_banner')
            ->where('banner_status', 1)
            ->field('banner_id, banner_image, banner_type, banner_goods_id, banner_content, banner_link')
            ->order('banner_sort desc')
            ->select();
        if($banner){
            return json(['status' => 200, 'msg' => $banner]);
        }else{
            return json(['status' => 0, 'msg' => []]);
        }
    }
    
    /**
     * 首页展示
     * @return \think\response\Json
     */
    public function index(){
        $data = [];
        // 热门商品
        $hot_goods = $this->hotGoods();
        if($hot_goods){
            foreach($hot_goods as $k => $v){
                $spec_price =  Db::table('up_goods_spec')
                    ->where('spec_goods_id',$v['goods_id'])
                    ->field('spec_price, spec_old_price')
                    ->order('spec_price asc')
                    ->find();
                $hot_goods[$k]['spec_price'] = $spec_price['spec_price'];
                $hot_goods[$k]['spec_old_price'] = $spec_price['spec_old_price'];
            }
            $data['hot_goods'] = $hot_goods;
        }else{
            $data['hot_goods'] = [];
        }
        // 秒杀商品
        $miaosha = $this->first_miaosha();
        if($miaosha){
            $spec = $this->miaoshaSpec($miaosha, 2);
            $miaosha['spec_price'] = $spec['spec_price'];
            $miaosha['spec_stock'] = $spec['spec_stock'];
            $miaosha['spec_sail_num'] = $spec['spec_sail_num'];
            $miaosha['spec_id'] = $spec['spec_id'];
            $miaosha['spec_name'] = $spec['spec_name'];
            $miaosha['spec_can'] = $spec['spec_can'];
            $miaosha['goods_status'] = $spec['goods_status'];
            $data['miaosha'] = $miaosha;
        }else{
            $data['miaosha'] = [];
        }
        // 拍卖商品
        $paimai = $this->paimai();
        if($paimai){
            $data['paimai'] = $paimai;
        }else{
            $data['paimai'] = [];
        }
        // 约稿
        $yue = $this->yue();
        if($yue){
            $data['yue'] = $yue;
        }else{
            $data['yue'] = [];
        }
        return json(['status' => 200, 'msg' => $data, 'now' => time()]);
    }
    
    /**
     * 关键字
     * @return \think\response\Json
     */
    public function keyword(){
        $keyword = Db::table('up_keyword')->field('keyword_id, keyword_name')->select();
        if($keyword){
            return json(['status' => 200, 'msg' => $keyword]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 搜索拍卖商品
     * @return \think\response\Json
     * goods_cate      商品种类
     * keyword         关键字
     * page            页码
     */
    public function searchPaimaiGoods(){
        $goods_cate = input('goods_cate');
        $keyword = input('keyword');
        $date = date('Y-m-d');
        if(isset($keyword) && $keyword != ''){
            $where['goods_name'] = ['like', '%' . $keyword . '%'];
            $where['goods_type'] = 2;
            $where['goods_status'] = 1;
            $where['goods_activity_date'] = $date;
            if(isset($goods_cate) && $goods_cate != 0){
                $where['goods_cate'] = $goods_cate;
            }
            $list = Db::table('up_goods')
                ->where($where)
                ->field('goods_id, goods_name, goods_thumb, goods_activity_start_time, goods_activity_end_time, 
            goods_now_price, goods_sta, goods_activity_date')
                ->order('goods_time desc')
                ->page($this->page, 15)
                ->select();
            if($list){
//            $now_time = strtotime(date('H:i')); // 当前时分时间戳
//            foreach($list as $kk => $vv){
//                if(strtotime($vv['goods_activity_start_time']) > $now_time){
//                    $list[$kk]['activity_status'] = 1; // 未开始
//                }elseif(strtotime($vv['goods_activity_start_time']) < $now_time && strtotime($vv['goods_activity_end_time']) > $now_time){
//                    $list[$kk]['activity_status'] = 2; // 进行中
//                }else{
//                    $list[$kk]['activity_status'] = 3; // 已结束
//                }
//            }
                return json(['status' => 200, 'msg' => $list]);
            }else{
                return json(['status' => 200, 'msg' => []]);
            }
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 搜索正常商品
     * @return \think\response\Json
     * goods_cate      商品种类
     * keyword         关键字
     * page            页码
     */
    public function searchCommonGoods(){
        $goods_cate = input('goods_cate');
        $keyword = input('keyword');
        if(isset($keyword) && $keyword != ''){
            $where['goods_name'] = ['like', '%' . $keyword . '%'];
            $where['goods_type'] = 1;
            $where['goods_status'] = 1;
            if(isset($goods_cate) && $goods_cate != 0){
                $where['goods_cate'] = $goods_cate;
            }
    
            $list = Db::table('up_goods')
                ->where($where)
                ->field('goods_id, goods_name, goods_thumb, goods_sail_num')
                ->order('goods_sail_num desc')
                ->page($this->page, 15)
                ->select();
            if($list){
                foreach($list as $k => $v){
                    $spec_price =  Db::table('up_goods_spec')
                        ->where('spec_goods_id',$v['goods_id'])
                        ->field('spec_price, spec_old_price')
                        ->order('spec_price asc')
                        ->find();
                    $list[$k]['spec_price'] = $spec_price['spec_price'];
                    $list[$k]['spec_old_price'] = $spec_price['spec_old_price'];
                }
                return json(['status' => 200, 'msg' => $list]);
            }else{
                return json(['status' => 200, 'msg' => []]);
            }
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 搜索约稿
     * @return \think\response\Json
     * keyword         关键字
     * page            页码
     */
    public function searchYue(){
        $keyword = input('keyword');
        
        if(isset($keyword) && $keyword != ''){
            $where['yue_status'] = 1;
            $where['yue_stat'] = 1;
            $where['yue_title'] = ['like', '%' . $keyword . '%'];
            $list = Db::table('up_yue')
                ->where($where)
                ->field('yue_id, yue_title, yue_day_num, yue_price, yue_date')
                ->order('yue_time desc')
                ->page($this->page, 15)
                ->select();
            if($list){
                return json(['status' => 200, 'msg' => $list]);
            }else{
                return json(['status' => 200, 'msg' => []]);
            }
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 更多热门商品
     * @return \think\response\Json
     * page    页码
     */
    public function moreHotGoods(){
        $list = $this->hotGoods(2);
        if($list){
            foreach($list as $k => $v){
                $spec_price =  Db::table('up_goods_spec')
                    ->where('spec_goods_id',$v['goods_id'])
                    ->field('spec_price, spec_old_price')
                    ->order('spec_price asc')
                    ->find();
                $list[$k]['spec_price'] = $spec_price['spec_price'];
                $list[$k]['spec_old_price'] = $spec_price['spec_old_price'];
            }
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 更多秒杀商品列表
     * @return \think\response\Json
     * start_time    场次开始时间  10:00格式
     * page          页码
     */
    public function moreMiaoshaGoods(){
        $start_time = input('start_time');
        $date = date('Y-m-d');
        $where['goods_type'] = 3;
        $where['goods_activity_date'] = $date;
        $where['goods_activity_start_time'] = $start_time;
        $where['goods_status'] = 1;
        $list = Db::table('up_goods')
            ->where($where)
            ->field('goods_id, goods_name, goods_thumb, goods_sta')
            ->page($this->page, 15)
            ->order('goods_time desc')
            ->select();
        if($list){
            $list = $this->miaoshaSpec($list, 1);
            return json(['status' => 200, 'msg' => $list, 'time' => time()]);
        }else{
            return json(['status' => 200, 'msg' => [], 'time' => time()]);
        }
    }
    
    /**
     * 更多拍卖商品列表
     * @return \think\response\Json
     * keyword      关键字
     * status       活动状态   0 全部状态  1 未开始   2 进行中   3 已结束
     * times        竞价次数排序   0 不排序   1 由高到低   2 由低到高
     * now_price    当前价格排序   0 不排序   1 由高到低   2 由低到高
     * page         页码
     */
    public function morePaimaiGoods(){
        $keyword = input('keyword');
        $status = input('status');
        $times = input('times');
        $now_price = input('now_price');
        $list = $this->paimai($keyword, $status, $times, $now_price, 2);
        if($list){
//            $now_time = strtotime(date('H:i')); // 当前时分时间戳
//            foreach($list as $k => $v){
//                if(strtotime($v['goods_activity_start_time']) > $now_time){
//                    $list[$k]['activity_status'] = 1; // 未开始
//                }elseif(strtotime($v['goods_activity_start_time']) < $now_time && strtotime($v['goods_activity_end_time']) > $now_time){
//                    $list[$k]['activity_status'] = 2; // 进行中
//                }else{
//                    $list[$k]['activity_status'] = 3; // 已结束
//                }
//            }
            return json(['status' => 200, 'msg' => $list, 'now' => time()]);
        }else{
            return json(['status' => 200, 'msg' => [], 'now' => time()]);
        }
    }
    
    /**
     * 更多约稿列表
     * @return \think\response\Json
     * page    页码
     * publish_time_sort     发布时间排序   0 不排序   1 倒序   2 正序
     * cycle_sort            周期排序      0 不排序    1 倒序   2 正序
     * price_sort            价格排序     0 不排序    1 倒序    2 正序
     */
    public function moreYue(){
        $publish_time_sort = input('publish_time_sort');
        $cycle_sort = input('cycle_sort');
        $price_sort = input('price_sort');
        $yue = $this->yue($publish_time_sort, $cycle_sort, $price_sort, 2);
        if($yue){
            return json(['status' => 200, 'msg' => $yue]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 秒杀商品或正常商品详情
     * @return \think\response\Json
     * goods_id     商品ID
     */
    public function GoodsDetail(){
        header("Access-Control-Allow-Origin: *");
        $goods_id = input('goods_id');
        $detail = Db::table('up_goods')
            ->where('goods_id', $goods_id)
            ->field('goods_id, goods_name, goods_thumb, goods_detail, goods_detail_images, goods_video, 
            goods_comment_num, goods_activity_date, goods_activity_start_time, goods_activity_end_time, 
            goods_type, goods_shop_id, goods_user_id, goods_sta')
            ->find();
        // 详情图片
        $image = trim($detail['goods_detail_images'], ',');
        if($image){
            $detail['goods_detail_images'] = explode(',', $image);
        }else{
            $detail['goods_detail_images'] = [];
        }
        
        // 规格、价格、销量
        if($detail['goods_type'] == 1){ // 正常商品
            $spec = Db::table('up_goods_spec')
                ->where('spec_goods_id', $detail['goods_id'])
                ->field('spec_id, spec_name, spec_price, spec_old_price, spec_can, spec_stock, spec_sail_num')
                ->order('spec_price asc')
                ->find();
        }else{
            $spec = $this->miaoshaSpec($detail, 2);
        }
        if($spec){
            $detail['spec'] = $spec;
        }else{
            $detail['spec'] = [];
        }
        
//        // 秒杀商品活动状态
//        if($detail['goods_type'] == 3){
//            $time = date('Y-m-d H:i');
//            $activity_begin_time = $detail['goods_activity_date'] . ' ' . $detail['goods_activity_start_time'];
//            $activity_end_time = $detail['goods_activity_date'] . ' ' . $detail['goods_activity_end_time'];
//            if($time < $activity_begin_time){
//                $detail['activity_status'] = 1; // 未开始
//            }elseif($time >= $activity_begin_time && $time < $activity_end_time){
//                $detail['activity_status'] = 2; // 进行中
//            }else{
//                $detail['activity_status'] = 3; // 已结束
//            }
//        }
        
        // 商品评价
        $comment = $this->goodsComment($detail['goods_id'], 1);
        if($comment){
            $comment_img = trim($comment['gc_images'], ',');
            if($comment_img){
                $comment['gc_images'] = explode(',', $comment_img);
            }else{
                $comment['gc_images'] = [];
            }
            $detail['comment'] = $comment;
        }else{
            $detail['comment'] = [];
        }
        
        // 店铺信息
        $detail['shop'] = Db::table('up_shop')->field('shop_id, shop_name, shop_logo')->where('shop_id', $detail['goods_shop_id'])->find();
        
        // 推荐商品  只放3个
        $tuijian_goods = Db::table('up_goods')
            ->where('goods_user_id', $detail['goods_user_id'])
            ->where('goods_id', 'NEQ', $detail['goods_id'])
            ->where('goods_status', 1)
            ->field('goods_id, goods_name, goods_thumb')
            ->order('goods_time desc')
            ->limit(3)
            ->select();
        if($tuijian_goods){
            foreach($tuijian_goods as $kt => $vt){
                $spec_tuijian = Db::table('up_goods_spec')
                    ->where('spec_goods_id', $detail['goods_id'])
                    ->field('spec_price, spec_old_price')
                    ->order('spec_price asc')
                    ->find();
                $tuijian_goods[$kt]['spec_price'] = $spec_tuijian['spec_price'];
                $tuijian_goods[$kt]['spec_old_price'] = $spec_tuijian['spec_old_price'];
            }
            $detail['tuijian_goods'] = $tuijian_goods;
        }else{
            $detail['tuijian_goods'] = [];
        }
        
        // 是否已收藏
        $collect = Db::table('up_my_collect')
            ->where('collect_goods_id', $detail['goods_id'])
            ->where('collect_my_id', $this->user_id)
            ->find();
        if($collect){
            $detail['is_collect'] = 1; // 已收藏
        }else{
            $detail['is_collect'] = 2; // 未收藏
        }
        // 商品浏览量+1
        Db::table('up_goods')->where('goods_id', $goods_id)->setInc('goods_look_num');
        return json(['status' => 200, 'msg' => $detail, 'now' => time()]);
    }
    
    /**
     * 收藏/取消收藏
     * @return \think\response\Json
     * goods_id     商品ID
     */
    public function doCollect(){
        $goods_id = input('goods_id');
        $collect = Db::table('up_my_collect')
            ->where('collect_my_id', $this->user_id)
            ->where('collect_goods_id', $goods_id)
            ->field('collect_id')
            ->find();
        if($collect){
            $res = Db::table('up_my_collect')->delete($collect['collect_id']);
        }else{
            $res = Db::table('up_my_collect')->insert([
                'collect_my_id' => $this->user_id,
                'collect_goods_id' => $goods_id,
                'collect_time' => $this->create_time
            ]);
        }
        if($res){
//            return json(['status' => 200, 'msg' => '操作成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->doSuccess[$this->language]]);
        }else{
//            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
            return json(['status' => 0, 'msg' => $this->msg_language->systemErr[$this->language]]);
        }
    }
    
    /**
     * 商品规格
     * @return \think\response\Json
     * goods_id     商品ID
     */
    public function goodsSpec(){
        $goods_id = input('goods_id');
        $goods_thumb = Db::table('up_goods')->where('goods_id', $goods_id)->value('goods_thumb');
        $spec = Db::table('up_goods_spec')
            ->where('spec_goods_id', $goods_id)
            ->field('spec_id, spec_name, spec_price, spec_old_price, spec_can, spec_stock')
            ->select();
        return json(['status' => 200, 'msg' => $spec, 'goods_thumb' => $goods_thumb]);
    }
    
    /**
     * 根据规格ID获取规格信息
     * @return \think\response\Json
     * spec_id    规格ID
     */
    public function getSpecById(){
        $spec_id = input('spec_id');
        $spec = Db::table('up_goods_spec')
            ->where('spec_id', $spec_id)
            ->field('spec_id, spec_name, spec_price, spec_old_price, spec_can, spec_stock')
            ->find();
        return json(['status' => 200, 'msg' => $spec]);
    }
    
    /**
     * 全部评价
     * @return \think\response\Json
     * goods_id   商品ID
     * comment_star   商品评价   0 全部   1 好评   2 差评
     * page       页码
     */
    public function goodsAllComment(){
        $goods_id = input('goods_id');
        $comment_star = input('comment_star');
        $comment_num = ['good_num' => 0, 'bad_num' => 0, 'total_num' => 0];
        $comment = $this->goodsComment($goods_id, 2, $comment_star);
        $commentAll = $this->goodsComment($goods_id, 2, 0, 2);
        // 评价数量
        if($commentAll){
            foreach($commentAll as $kk => $vv){
                if($vv['gc_star'] >= 3){
                    $comment_num['good_num'] += 1;
                }else{
                    $comment_num['bad_num'] += 1;
                }
                $comment_num['total_num'] += 1;
            }
        }
        if($comment){
            foreach($comment as $k => $v){
                // 图片
                $comment_img = trim($v['gc_images'], ',');
                if($comment_img){
                    $comment[$k]['gc_images'] = explode(',', $comment_img);
                }else{
                    $comment[$k]['gc_images'] = [];
                }
            }
            
            return json(['status' => 200, 'msg' => $comment, 'comment_num' => $comment_num]);
        }else{
            return json(['status' => 200, 'msg' => [], 'comment_num' => $comment_num]);
        }
    }
    
    /**
     * 默认地址
     * @return \think\response\Json
     */
    public function defaultAddress(){
        $address = Db::table('up_user_address')
            ->join('up_area ua1', 'ua1.district_id = addr_sheng', 'LEFT')
            ->join('up_area ua2', 'ua2.district_id = addr_shi', 'LEFT')
            ->join('up_area ua3', 'ua3.district_id = addr_qu', 'LEFT')
            ->where('addr_user_id', $this->user_id)
            ->where('addr_del', 1)
            ->field('addr_id, addr_name, addr_phone, ua1.district sheng_name, ua2.district shi_name, 
            ua3.district qu_name, addr_address')
            ->find();
        if($address){
            return json(['status' => 200, 'msg' => $address, 'addr_id' => $address['addr_id']]);
        }else{
            return json(['status' => 200, 'msg' => [], 'addr_id' => '']);
        }
    }
    
    /**
     * 提交订单页面
     * @return \think\response\Json
     * spec_id    规格ID
     * goods_id   商品ID
     */
    public function showOrder(){
        $spec_id = input('spec_id');
        $goods_id = input('goods_id');
        $goods = Db::table('up_goods')
            ->where('goods_id', $goods_id)
            ->field('goods_id, goods_name, goods_thumb')
            ->find();
        $spec = Db::table('up_goods_spec')
            ->where('spec_id', $spec_id)
            ->field('spec_id, spec_name, spec_price, spec_old_price, spec_can')
            ->find();
        $spec['goods_id'] = $goods['goods_id'];
        $spec['goods_name'] = $goods['goods_name'];
        $spec['goods_thumb'] = $goods['goods_thumb'];
        return json(['status' => 200, 'msg' => $spec]);
    }
    
    /**
     * 获取符合条件的优惠券
     * @return \think\response\Json
     * goods_price    订单价格
     */
    public function myCoupon(){
        $goods_price = input('goods_price');
        $date = date('Y-m-d');
        $coupon = Db::table('up_my_coupon')
            ->join('up_coupon', 'coupon_id = mc_coupon_id', 'LEFT')
            ->where('mc_user_id', $this->user_id)
            ->where('mc_status', 1)
            ->where('mc_use_start_time', '<=', $date)
            ->where('mc_use_end_time', '>=', $date)
            ->where(function ($query) use($goods_price){
                $query->where(function ($query1) use($goods_price){
                    $query1->where('coupon_type', 1)
                        ->where('mc_use_price', '<', $goods_price);
                })
                ->whereOr(function ($query2){
                    $query2->where('coupon_type', 2);
                });
            })
            ->field('mc_id, mc_use_price, mc_dikou_price, mc_use_start_time, mc_use_end_time, coupon_type, 
            coupon_name')
            ->order('mc_ling_time desc')
            ->select();
        if($coupon){
            return json(['status' => 200, 'msg' => $coupon]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 提交订单
     * @return \think\response\Json
     * goods_id      商品ID
     * spec_id       规格ID
     * order_num     订单数量
     * my_coupon_id   优惠券ID   如果没有优惠券，传0
     * order_liuyan   留言
     */
    public function commitOrder(){
        $goods_id = input('goods_id');
        $spec_id = input('spec_id');
        $order_num = input('order_num'); // 订单数量
        $my_coupon_id = input('my_coupon_id');
        $order_liuyan = input('order_liuyan'); // 留言
        $order_addr_id = input('order_addr_id');
        $order_number = date('YmdHis') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT); // 订单号
        // 商品
        $goods = Db::table('up_goods')->where('goods_id', $goods_id)->field('goods_type, goods_user_id')->find();
        // 规格
        $spec = Db::table('up_goods_spec')->where('spec_id', $spec_id)->field('spec_stock, spec_old_price, spec_price')->find();
        
//        if($spec['spec_stock'] < $order_num) return json(['status' => 0, 'msg' => '库存不足']);
//        if($goods['goods_user_id'] == $this->user_id) return json(['status' => 0, 'msg' => '不可以购买自己的商品']);
        if($spec['spec_stock'] < $order_num) return json(['status' => 0, 'msg' => $this->msg_language->stockLose[$this->language]]);
        if($goods['goods_user_id'] == $this->user_id) return json(['status' => 0, 'msg' => $this->msg_language->refuseBuySelfGoods[$this->language]]);
        
        // 总价
//        if(empty($spec['spec_old_price'])){
//            $price = $spec['spec_price'];
//        }else{
//            $price = $spec['spec_old_price'];
//        }
        $total_price = $spec['spec_price'] * $order_num;
        
        $order = [
            'order_number' => $order_number,
            'order_goods_price' => $total_price,
            'order_num' => $order_num,
            'order_goods_id' => $goods_id,
            'order_buy_user_id' => $this->user_id,
            'order_sail_user_id' => $goods['goods_user_id'],
            'order_type' => $goods['goods_type'],
            'order_spec_id' => $spec_id,
            'order_liuyan' => $order_liuyan,
            'order_create_time' => $this->create_time,
            'order_addr_id' => $order_addr_id
        ];
        
        // 实付价格 如果有优惠券，减去优惠券
        if($my_coupon_id != 0){
            // 优惠券
            $coupon = Db::table('up_my_coupon')->where('mc_id', $my_coupon_id)->field('mc_dikou_price')->find();
            $pay_price = $total_price - $coupon['mc_dikou_price'];
            $order['order_is_coupon'] = 1;
            $order['order_my_coupon_id'] = $my_coupon_id;
            $order['order_coupon_price'] = $coupon['mc_dikou_price'];
            // 如果使用了优惠券后，实际支付价格小于0，则订单状态为已支付,并且不可以申请售后
            if($pay_price < 0){
                $pay_price = 0;
                $order['order_status'] = 2;
                $order['order_pay_time'] = $this->create_time;
                $order['order_pay_date'] = date('Y-m-d');
                $order['order_pay_type'] = -1;
                $order['order_aftersale_status'] = 2;
            }
        }else{
            $pay_price = $total_price;
            $order['order_is_coupon'] = 2;
            $order['order_my_coupon_id'] = 0;
            $order['order_coupon_price'] = 0;
        }
        $order['order_pay_price'] = $pay_price;
        
        Db::startTrans();
        try{
            // 插入订单表
            $order_id = Db::table('up_goods_order')->insertGetId($order);
            
            // 如果实付价格为0
            if($pay_price == 0){
                // 给画师发个信息
                $notice = [
                    'notice_title' => '您有一个订单已付款',
                    'notice_content' => '您有一个订单已付款',
                    'notice_user' => ',' . $order['order_sail_user_id'] . ',',
                    'notice_type' => 5,
                    'notice_order_id' => $order_id,
                    'notice_read_user' => ',0,',
                    'notice_del_user' => ',0,',
                    'notice_time' => date('Y-m-d H:i:s')
                ];
                Db::table('up_notice')->insert($notice);
            }
            
            // 修改优惠券状态
            if($my_coupon_id != 0){
                Db::table('up_my_coupon')->where('mc_id', $my_coupon_id)->update(['mc_use_time' => date('Y-m-d'), 'mc_status' => 2]);
            }
            
            // 减库存
            Db::table('up_goods_spec')->where('spec_id', $spec_id)->setDec('spec_stock', $order_num);
            Db::commit();
            $order_info = Db::table('up_goods_order')->where('order_id', $order_id)->field('order_id, order_pay_price, order_number')->find();
            return json(['status' => 200, 'msg' => $order_info]);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 拍卖商品详情
     * @return \think\response\Json
     * goods_id    商品ID
     */
    public function paimaiDetail(){
        header("Access-Control-Allow-Origin: *");
        $goods_id = input('goods_id');
        $detail = Db::table('up_goods')
            ->where('goods_id', $goods_id)
            ->field('goods_id, goods_name, goods_thumb, goods_spec_min_price, goods_detail, goods_detail_images, 
            goods_video, goods_type, goods_activity_date, goods_activity_start_time, goods_activity_end_time, 
            goods_bz_price, goods_fudu, goods_now_price, goods_now_times, goods_spec_name, goods_shop_id, goods_sta')
            ->find();
        // 详情图片
        $image = trim($detail['goods_detail_images'], ',');
        if($image){
            $detail['goods_detail_images'] = explode(',', $image);
        }else{
            $detail['goods_detail_images'] = [];
        }
        
        // 活动状态
//        $time = date('Y-m-d H:i');
//        $activity_begin_time = $detail['goods_activity_date'] . ' ' . $detail['goods_activity_start_time'];
//        $activity_end_time = $detail['goods_activity_date'] . ' ' . $detail['goods_activity_end_time'];
//        if($time < $activity_begin_time){
//            $detail['activity_status'] = 1; // 未开始
//        }elseif($time >= $activity_begin_time && $time < $activity_end_time){
//            $detail['activity_status'] = 2; // 进行中
//        }else{
//            $detail['activity_status'] = 3; // 已结束
//        }
        
        // 是否已交保证金
        $exist = Db::table('up_goods_order')
            ->where('order_buy_user_id', $this->user_id)
            ->where('order_goods_id', $goods_id)
            ->where('order_type', 5)
            ->field('order_status, order_id, order_pay_price')
            ->order('order_id desc')
            ->find();
        if($exist){
            if($exist['order_status'] == 1){ // 有订单，未付款
                $detail['order_status'] = 1;
                $detail['order_id'] = $exist['order_id'];
                $detail['order_pay_price'] = $exist['order_pay_price'];
            }
            if($exist['order_status'] == 2){ // 有订单，已付款
                $detail['order_status'] = 2;
            }
        }else{ // 没订单
            $detail['order_status'] = 3;
        }
        
        // 店铺信息
        $detail['shop'] = Db::table('up_shop')->field('shop_id, shop_name, shop_logo')->where('shop_id', $detail['goods_shop_id'])->find();
        
        // 竞拍信息
        $detail['jing'] = $this->jingList($goods_id);
        // 商品浏览量+1
        Db::table('up_goods')->where('goods_id', $goods_id)->setInc('goods_look_num');
        return json(['status' => 200, 'msg' => $detail, 'now' => time()]);
    }
    
    /**
     * 竞价列表
     * @return \think\response\Json
     * goods_id   商品ID
     * page       页码
     */
    public function jingDetail(){
        $goods_id = input('goods_id');
        $detail =$this->jingList($goods_id, 2);
        if($detail){
            return json(['status' => 200, 'msg' => $detail]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 竞价规则和协议
     * @return \think\response\Json
     * type    9 竞价规则   10 竞价协议
     */
    public function ruleAndAgreement(){
        $type = input('type');
        if($type == 9){
            $field = 'other_jing_rule';
        }else{
            $field = 'other_jing_agreement';
        }
        $data = Db::table('up_other')->where('other_id', $type)->value($field);
        return json(['status' => 200, 'msg' => $data]);
    }
    
    /**
     * 提交押金页面
     * @return \think\response\Json
     * goods_id    商品ID
     */
    public function showYJ(){
        $goods_id = input('goods_id');
        $data = [];
        // 是否有未付款的押金订单
        $exist = Db::table('up_goods_order')
            ->where('order_buy_user_id', $this->user_id)
            ->where('order_goods_id', $goods_id)
            ->where('order_type', 5)
            ->where('order_status', 1)
            ->field('order_id, order_status')
            ->order('order_id desc')
            ->find();
        if($exist){
           $data['order_id'] = $exist['order_id'];
        }else{ // 没订单
            $data['order_id'] = 0;
        }
        $data['yj'] = Db::table('up_goods')
            ->where('goods_id', $goods_id)
            ->value('goods_bz_price');
        return json(['status' => 200, 'msg' => $data]);
    }
    
    /**
     * 拍卖商品提交押金
     * @return \think\response\Json
     * goods_id      商品ID
     * order_addr_id   收货地址ID
     * order_id        订单ID   有未付款订单，返回订单ID，没有返回空
     */
    public function commitYaJ(){
        $goods_id = input('goods_id');
        $order_addr_id = input('order_addr_id');
        $order_id = input('order_id');

        // 如果order_id = ''，则插入订单，如果没有则更新订单
        if($order_id == ''){
            $order_number = date('YmdHis') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT); // 订单号
            // 商品
            $goods = Db::table('up_goods')->where('goods_id', $goods_id)->field('goods_type, goods_user_id, goods_bz_price')->find();
            // 规格
            $spec = Db::table('up_goods_spec')->where('spec_goods_id', $goods_id)->field('spec_id')->find();
            
//            if($goods['goods_user_id'] == $this->user_id) return json(['status' => 0, 'msg' => '不可以竞拍自己的商品']);
            if($goods['goods_user_id'] == $this->user_id) return json(['status' => 0, 'msg' => $this->msg_language->refuseJingSelfGoods[$this->language]]);
            
            $order = [
                'order_number' => $order_number,
                'order_goods_price' => $goods['goods_bz_price'],
                'order_pay_price' => $goods['goods_bz_price'],
                'order_paimai_ya_price' => $goods['goods_bz_price'],
                'order_num' => 1,
                'order_goods_id' => $goods_id,
                'order_buy_user_id' => $this->user_id,
                'order_sail_user_id' => $goods['goods_user_id'],
                'order_type' => 5,
                'order_spec_id' => $spec['spec_id'],
                'order_is_coupon' => 2,
                'order_my_coupon_id' => 0,
                'order_coupon_price' => 0,
                'order_addr_id' => $order_addr_id,
                'order_create_time' => $this->create_time
            ];
            // 插入订单表
            $res = Db::table('up_goods_order')->insertGetId($order);
            if($res){
                $order_info = Db::table('up_goods_order')->where('order_id', $res)->field('order_id, order_pay_price, order_number')->find();
                return json(['status' => 200, 'msg' => $order_info]);
            }else{
//                return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
                return json(['status' => 0, 'msg' => $this->msg_language->systemErr[$this->language]]);
            }
        }else{
            $res = Db::table('up_goods_order')
                ->where('order_id', $order_id)
                ->update([
                    'order_addr_id' => $order_addr_id,
                    'order_updatetime' => $this->create_time
                ]);
            if($res){
                $order_info = Db::table('up_goods_order')->where('order_id', $order_id)->field('order_id, order_pay_price')->find();
                return json(['status' => 200, 'msg' => $order_info]);
            }else{
//                return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
                return json(['status' => 0, 'msg' => $this->msg_language->systemErr[$this->language]]);
            }
        }
    }
    
    /**
     * 竞价
     * @return \think\response\Json
     * goods_id    商品ID
     */
    public function addPrice(){
        $goods_id = input('goods_id');
        $goods = Db::table('up_goods')
            ->where('goods_id', $goods_id)
            ->field('goods_fudu, goods_now_price, goods_activity_end_datetime')
            ->find();
        
        $time = date('Y-m-d H:i');
        if(strtotime($time) > strtotime($goods['goods_activity_end_datetime'])) return json(['status' => 0, 'msg' => '活动已结束']);
        
        // 加价之后的价格
        $new_price = $goods['goods_now_price'] + $goods['goods_fudu'];
        $jing = [
            'gj_user_id' => $this->user_id,
            'gj_goods_id' => $goods_id,
            'gj_price' => $goods['goods_fudu'],
            'gj_old_price' => $goods['goods_now_price'],
            'gj_new_price' => $new_price,
            'gj_datetime' => $this->create_time,
            'gj_status' => 1
        ];
        Db::startTrans();
        try{
            // 修改竞价表竞价记录的状态
            Db::table('up_goods_jing')
                ->where('gj_goods_id', $goods_id)
                ->where('gj_status', 1)
                ->setField('gj_status', 2);
            // 插入竞价表
            Db::table('up_goods_jing')->insert($jing);
            // 更新商品表
            Db::table('up_goods')->where('goods_id', $goods_id)->setInc('goods_now_price', $goods['goods_fudu']);
            Db::table('up_goods')->where('goods_id', $goods_id)->setInc('goods_now_times');
            Db::commit();
//            return json(['status' => 200, 'msg' => '竞价成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->jingSuccess[$this->language]]);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 应征约稿
     * @return \think\response\Json
     * yue_id     约稿ID
     */
    public function recruited(){
        $yue_id = input('yue_id');
        $user = Db::table('up_u_user')->where('user_id', $this->user_id)->field('user_role')->find();
//        if($user['user_role'] == 1) return json(['status' => 0, 'msg' => '您还不是画师']);
        if($user['user_role'] == 1) return json(['status' => 0, 'msg' => $this->msg_language->notPainter[$this->language]]);
        $yue = Db::table('up_yue')->where('yue_id', $yue_id)->field('yue_user_id')->find();
//        if($yue['yue_user_id'] == $this->user_id) return json(['status' => 0, 'msg' => '不可以应征自己的约稿']);
        if($yue['yue_user_id'] == $this->user_id) return json(['status' => 0, 'msg' => $this->msg_language->canNotApply[$this->language]]);
        $insert = [
            'yju_user_id' => $this->user_id,
            'yju_yue_id' => $yue_id,
            'yju_time' => $this->create_time
        ];
        $exist = Db::table('up_yue_join_user')
            ->where('yju_yue_id', $yue_id)
            ->where('yju_user_id', $this->user_id)
            ->find();
        Db::startTrans();
        try{
            // 约稿应征表
            if($exist){
                Db::table('up_yue_join_user')
                    ->where('yju_yue_id', $yue_id)
                    ->where('yju_user_id', $this->user_id)
                    ->update([
                        'yju_status' => 1,
                        'yju_time' => $this->create_time,
                        'yju_commit_time' => null
                    ]);
            }else{
                Db::table('up_yue_join_user')->insert($insert);
            }
            
            // 约稿状态 改成已有应征人员
            Db::table('up_yue')->where('yue_id', $yue_id)->setField('yue_is_call', 2);
            Db::commit();
//            return json(['status' => 200, 'msg' => '应征成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->applySuccess[$this->language]]);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 约稿举报类型
     * @return \think\response\Json
     */
    public function yueReportType(){
        $data = Db::table('up_yue_report_type')
            ->where('yue_report_del', 1)
            ->field('yue_report_type_id, yue_report_type_name')
            ->order('yue_report_type_time desc')
            ->select();
        return json(['status' => 200, 'msg' => $data]);
    }
    
    /**
     * 约稿举报
     * @return \think\response\Json
     * yue_id     约稿ID
     * yue_report_type_id    举报类型ID
     */
    public function report(){
        $yue_id = input('yue_id');
        $yue_report_type_id = input('yue_report_type_id');
        $yue = Db::table('up_yue')->where('yue_id', $yue_id)->field('yue_user_id')->find();
        $insert = [
            'report_yue_id' => $yue_id,
            'report_user_id' => $this->user_id,
            'report_other_user_id' => $yue['yue_user_id'],
            'report_yue_type_id' => $yue_report_type_id,
            'report_time' => $this->create_time
        ];
        $res = Db::table('up_yue_report')->insert($insert);
        if($res){
//            return json(['status' => 200, 'msg' => "提交成功"]);
            return json(['status' => 200, 'msg' => $this->msg_language->commitSuccess[$this->language]]);
        }else{
//            return json(['status' => 100, 'msg' => "系统异常，请稍后重试"]);
            return json(['status' => 100, 'msg' => $this->msg_language->systemErr[$this->language]]);
        }
    }
    
    /**
     * 热门推荐商品
     * @param $show_type    1 首页展示两条    2 更多热门商品
     * @return $this|false|\PDOStatement|string|\think\Collection
     */
    private function hotGoods($show_type = 1){
        $list = Db::table('up_goods')
            ->where('goods_type', 1)
            ->where('goods_status', 1)
            ->where('goods_tuijian', 1)
            ->field('goods_id, goods_name, goods_thumb, goods_sail_num')
            ->order('goods_hot_sort asc');
            if($show_type == 1){
                $list->limit(2);
            }else{
                $list->page($this->page, 15);
            }
            $list = $list->select();
        return $list;
    }
    
    /**
     * 秒杀商品
     * @return array|false|\PDOStatement|string|\think\Model
     */
    private function first_miaosha(){
        $date = date('Y-m-d'); // 当天日期
        $time = date('H:i'); // 当前时间
        $time_now = $date . ' ' . $time;
        $first_start_time = $date . ' 10:00'; // 当天第一场开始时间
        $last_end_time = $date . ' 19:00'; // 当天最后一场结束时间
        if(strtotime($time_now) < strtotime($first_start_time)){ // 第一场未开始
            // 当天参加活动最近的一个商品
            $data = Db::table('up_goods')
                ->where('goods_type', 3)
                ->where('goods_status', 1)
                ->where('goods_activity_date', $date)
                ->order('goods_activity_start_time asc')
                ->field('goods_id, goods_name, goods_thumb, goods_activity_start_time, goods_sta, goods_activity_date')
                ->find();
//            if($data) $data['activity_status'] = 1; // 未开始
        }elseif(strtotime($time_now) > strtotime($last_end_time)){ // 已结束
            // 当天参加活动的最后一个商品
            $data = Db::table('up_goods')
                ->where('goods_type', 3)
                ->where('goods_status', 1)
                ->where('goods_activity_date', $date)
                ->order('goods_activity_end_time desc')
                ->field('goods_id, goods_name, goods_thumb, goods_activity_start_time, goods_sta, goods_activity_date')
                ->find();
//            if($data) $data['activity_status'] = 3; // 已结束
        }else{ // 进行中
            // 参加活动结束时间大于当前时间的商品
            $data = Db::table('up_goods')
                ->where('goods_type', 3)
                ->where('goods_status', 1)
                ->where('goods_activity_date', $date)
                ->where('goods_activity_end_time', '>', $time)
                ->order('goods_activity_start_time asc')
                ->field('goods_id, goods_name, goods_thumb, goods_activity_start_time, goods_activity_end_time, goods_sta, goods_activity_date')
                ->find();
//            if($data){
//                $data['activity_status'] = 2; // 进行中
//            }else{
//                $data = Db::table('up_goods')
//                    ->where('goods_type', 3)
//                    ->where('goods_activity_date', $date)
//                    ->where('goods_activity_end_time', '<', $time)
//                    ->order('goods_activity_end_time desc')
//                    ->field('goods_id, goods_name, goods_thumb, goods_activity_start_time, goods_activity_end_time')
//                    ->find();
//                if($data) $date['activity_status'] = 3; // 已结束
//            }
            if(!$data){
                $data = Db::table('up_goods')
                    ->where('goods_type', 3)
                    ->where('goods_status', 1)
                    ->where('goods_activity_date', $date)
                    ->where('goods_activity_end_time', '<', $time)
                    ->order('goods_activity_end_time desc')
                    ->field('goods_id, goods_name, goods_thumb, goods_activity_start_time, 
                    goods_activity_end_time, goods_sta, goods_activity_date')
                    ->find();
            }
        }
        return $data;
    }
    
    /**
     * 拍卖商品
     * @param string $keyword    搜索关键字   没有就传空
     * @param int $status        活动状态   0 全部  1 未开始   2 进行中   3 已结束
     * @param int $times         竞价次数   0 全部  1 次数降序   2 次数升序
     * @param int $now_price     当前价格   0 全部  1 价格降序   2 价格升序
     * @param int $show_type     展示页面   1 首页两条   2 更多拍卖商品
     * @return $this|false|\PDOStatement|string|\think\Collection
     */
    private function paimai($keyword = '', $status = 0, $times = 0, $now_price = 0, $show_type = 1){
        $date = date('Y-m-d');
        $where['goods_activity_date'] = $date;
        $where['goods_type'] = 2;
        if(!empty($keyword)){
            $where['goods_name'] = ['like', '%' . $keyword . '%'];
        }
        if($status != 0){
            if($status == 1){ // 未开始
                $where['goods_sta'] = 1;
            }elseif($status == 2){ // 进行中
                $where['goods_sta'] = 2;
            }else{ // 已结束
                $where['goods_sta'] = 3;
            }
        }
        // 排序
        $order = 'goods_time desc';
        if($times != 0){
            if($times == 1){ // 出价次数降序
                $order .= ', goods_now_times desc';
            }else{ // 出价次数升序
                $order .= ', goods_now_times asc';
            }
        }
        if($now_price != 0){
            if($now_price == 1){ // 当前竞价降序
                $order .= ', goods_now_price desc';
            }else{ // 出价次数升序
                $order .= ', goods_now_price asc';
            }
        }
        
        $data = Db::table('up_goods')
            ->where($where)
            ->field('goods_id, goods_name, goods_thumb, goods_activity_start_time, goods_activity_end_time, 
            goods_now_price, goods_sta, goods_activity_date')
            ->order($order);
            if($show_type == 1){
                $data->limit(2);
            }else{
                $data->page($this->page, 15);
            }
            $data = $data->select();
        return $data;
    }
    
    /**
     * 约稿
     * @param int $publish_time_sort    时间排序   0 不排序，默认降序   1 降序   2 升序
     * @param int $cycle_sort           周期       0  不排序   1 降序   2 升序
     * @param int $price_sort           价格       0  不排序   1 降序   2 升序
     * @param int $show_type            展示页面   1 首页3条    2 更多约稿
     * @return $this|false|\PDOStatement|string|\think\Collection
     */
    private function yue($publish_time_sort = 0, $cycle_sort = 0, $price_sort = 0, $show_type = 1){
        $where['yue_status'] = 1;
        $where['yue_stat'] = 1;
        // 排序
        $order = 'yue_time desc';
        if($publish_time_sort != 0){
            if($publish_time_sort == 1){ // 发布日期降序
                $order = 'yue_time desc';
            }else{
                $order = 'yue_time asc';
            }
        }
        if($cycle_sort != 0){
            if($cycle_sort == 1){ // 周期降序
                $order = 'yue_day_num desc';
            }else{
                $order = 'yue_day_num asc';
            }
        }
        if($price_sort != 0){
            if($price_sort == 1){ // 价格降序
                $order = 'yue_price desc';
            }else{
                $order = 'yue_price asc';
            }
        }
        $data = Db::table('up_yue')
            ->where($where)
            ->field('yue_id, yue_title, yue_day_num, yue_price, yue_date')
            ->order($order);
        if($show_type == 1){
            $data->limit(3);
        }else{
            $data->page($this->page, 15);
        }
        $data = $data->select();
        return $data;
    }
    
    /**
     * 秒杀商品是否售罄&规格库存
     * @param $list    商品列表||商品详情数组
     * @param $type    1 商品列表   2 商品详情
     * @return mixed
     */
    private function miaoshaSpec($list, $type = 1){
        $flag = 1; // 假设全部卖完了
        if($type == 2){ // 详情
            $spec = [];
            $spec_price =  Db::table('up_goods_spec')
                ->where('spec_goods_id',$list['goods_id'])
                ->field('spec_price, spec_old_price, spec_stock, spec_sail_num, spec_id, spec_name, spec_can')
                ->order('spec_price asc')
                ->select();
            foreach($spec_price as $k => $v){
                if($v['spec_stock'] != 0){ // 没有卖完的规格
                    $spec['spec_price'] = $v['spec_price'];
                    $spec['spec_stock'] = $v['spec_stock'];
                    $spec['spec_sail_num'] = $v['spec_sail_num'];
                    $spec['spec_id'] = $v['spec_id'];
                    $spec['spec_name'] = $v['spec_name'];
                    $spec['spec_can'] = $v['spec_can'];
                    $spec['spec_old_price'] = $v['spec_old_price'];
                    $flag = 2; // 没有售罄
                    break;
                }
            }
            if($flag == 1){
                $spec_price =  Db::table('up_goods_spec')
                    ->where('spec_goods_id',$list['goods_id'])
                    ->field('spec_price, spec_old_price, spec_stock, spec_sail_num, spec_id, spec_name, spec_can')
                    ->order('spec_price asc')
                    ->find();
                $spec['spec_price'] = $spec_price['spec_price'];
                $spec['spec_stock'] = $spec_price['spec_stock'];
                $spec['spec_sail_num'] = $spec_price['spec_sail_num'];
                $spec['spec_id'] = $spec_price['spec_id'];
                $spec['spec_name'] = $spec_price['spec_name'];
                $spec['spec_can'] = $spec_price['spec_can'];
                $spec['spec_old_price'] = $spec_price['spec_old_price'];
            }
            $spec['goods_status'] = $flag;
            return $spec;
        }else{
            foreach($list as $k => $v){
                $spec_price =  Db::table('up_goods_spec')
                    ->where('spec_goods_id',$v['goods_id'])
                    ->field('spec_price, spec_old_price, spec_stock, spec_sail_num, spec_id, spec_old_price, 
                    spec_can, spec_name')
                    ->order('spec_price asc')
                    ->select();
                foreach($spec_price as $kk => $vv){
                    if($vv['spec_stock'] != 0){ // 没有卖完的规格
                        $list[$k]['spec_price'] = $vv['spec_price'];
                        $list[$k]['spec_stock'] = $vv['spec_stock'];
                        $list[$k]['spec_sail_num'] = $vv['spec_sail_num'];
                        $list[$k]['spec_id'] = $vv['spec_id'];
                        $list[$k]['spec_old_price'] = $vv['spec_old_price'];
                        $list[$k]['spec_can'] = $vv['spec_can'];
                        $list[$k]['spec_name'] = $vv['spec_name'];
                        $flag = 2; // 没有售罄
                        break;
                    }
                }
                if($flag == 1){
                    $spec_price_min =  Db::table('up_goods_spec')
                        ->where('spec_goods_id',$v['goods_id'])
                        ->field('spec_price, spec_old_price, spec_stock, spec_sail_num, spec_id, spec_old_price, 
                        spec_can, spec_name')
                        ->order('spec_price asc')
                        ->find();
                    $list[$k]['spec_price'] = $spec_price_min['spec_price'];
                    $list[$k]['spec_stock'] = $spec_price_min['spec_stock'];
                    $list[$k]['spec_sail_num'] = $spec_price_min['spec_sail_num'];
                    $list[$k]['spec_id'] = $spec_price_min['spec_id'];
                    $list[$k]['spec_old_price'] = $spec_price_min['spec_old_price'];
                    $list[$k]['spec_can'] = $spec_price_min['spec_can'];
                    $list[$k]['spec_name'] = $spec_price_min['spec_name'];
                }
                $list[$k]['goods_status'] = $flag;
            }
            return $list;
        }
    }
    
    /**
     * 商品的评价
     * @param $goods_id    商品ID
     * @param $show_type   展示页面   1 商品详情展示1条    2 商品评价页面
     * @param $comment_star   评价   0 全部   1 好评  2 差评
     * @param $no_page      是否需要分页    1 需要   2 不需要
     * @return array|false|\PDOStatement|string|\think\Model
     */
    private function goodsComment($goods_id, $show_type = 1, $comment_star = 0, $no_page = 1){
        $comment = Db::table('up_goods_comment')
            ->join('up_u_user', 'user_id = gc_user_id', 'LEFT')
            ->join('up_goods_spec', 'gc_spec_id = spec_id', 'LEFT')
            ->field('gc_id, user_image, user_username, gc_star, gc_time, spec_name, spec_can, gc_content, 
            gc_images')
            ->where('gc_goods_id', $goods_id)
            ->order('gc_time desc');
        if($show_type == 1){
            $comment = $comment->find();
        }else{
            if($comment_star != 0){
                $comment->where('gc_type', $comment_star);
            }
            if($no_page == 1){
                $comment->page($this->page, 15);
            }
            $comment = $comment->select();
        }
        return $comment;
    }
    
    /**
     * 竞拍列表
     * @param $goods_id        商品ID
     * @param int $show_type   展示页面    1 商品详情展示5条   2 全部竞拍
     * @return $this|false|\PDOStatement|string|\think\Collection
     */
    private function jingList($goods_id, $show_type = 1){
        $jing = Db::table('up_goods_jing')
            ->join('up_u_user', 'user_id = gj_user_id', 'LEFT')
            ->where('gj_goods_id', $goods_id)
            ->field('user_username, gj_status, gj_datetime, gj_price')
            ->order('gj_datetime desc');
        if($show_type == 1){
            $jing->limit(5);
        }else{
            $jing->page($this->page, 15);
        }
        $jing = $jing->select();
        return $jing;
    }
}